//
//  WXPToCPIListVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/8.
//

#import "WXPToCPIListVC.h"
#import "WXPToCPIListCell.h"

@interface WXPToCPIListVC ()

@property (nonatomic, strong) WXPNavView * navView;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;

@end

@implementation WXPToCPIListVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (!_navView) {
        _navView = [WXPNavView new];
        [_navView resetViewWithTitle:@"Productos recomendados"];
        _navView.leftTop = XY(0, 0);
    }
    return _navView;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_CpiProduct action:KEY_Enter extInfo:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[WXPToCPIListCell class] forCellReuseIdentifier:NSStringFromClass([WXPToCPIListCell class])];
    [self configUI];
    [self requestList];
}

- (void)configUI{
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:self.navView];
    [self.view addSubview:self.agreementView];
    self.tableView.tableFooterView = ^{
        UIView *view = [UIView new];
        view.height = SCREEN_HEIGHT-self.agreementView.top;
        return view;
    }();
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.aryDatas.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WXPToCPIListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WXPToCPIListCell class]) forIndexPath:indexPath];
    [cell resetCellWithModel:self.aryDatas[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [WXPToCPIListCell getHeight:self.aryDatas[indexPath.row] className:@"" selectorName:@"resetCellWithModel:"];
}
//table header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
//table footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

#pragma mark - request
- (void)requestList{
    [RequestApi requestProductCpi2WithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        if (self.isRemoveAll) {
            [self.aryDatas removeAllObjects];
        }
        
        NSArray * aryResponse = [GlobalMethod exchangeDic:[response arrayValueForKey:KEY_productList] toAryWithModelName:@"ModelProductCpi"];
        if(aryResponse.count < 10){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        self.pageNum++;
        if (isAry(aryResponse)) {
            [self.aryDatas addObjectsFromArray: aryResponse];
        }else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
